/* 
 * jSonComments
 * Author:  Christoph Stickel <mixer2@gmail.com>
 * License: Released under MIT License
 * 
 */

(function($) {
		
$.fn.jSonComments = function(options) {
	var settings = $.extend({}, $.fn.jSonComments.defaults, options);
	
	if(!this.data(settings.dataAttribute) || !settings.cache) {
		var metadata=$(this).html().match(new RegExp('<!-- *'+settings.keyword+'((.|\n|\r)*?)-->','i'));
		
		if(metadata) {
			try {
				var data = $.parseJSON(metadata[1]);
			} catch(e) {
				throw {"type": 1,"toString":function() { return e; }};
			}
			
			$(this).data(settings.dataAttribute, data);
		} else {
			var selector = $(this).selector;
			throw {"type": 2,"toString":function() { return "No JSON Comment found. Please check if "+selector+" contains <!-- "+settings.keyword+" -->."; }};
		}
	}
	
	return this.data(settings.dataAttribute);
};

$.fn.jSonComments.defaults = {
	    'dataAttribute': 'jSonCommentsData',
	    'keyword': 'Metadata',
	    'cache': 'true'
	  };

})(jQuery);